package com.org.gundutubruk;

import android.content.Context;
import android.media.MediaPlayer;

public class GameSound {
	private static MediaPlayer menumusic;
	private static MediaPlayer gamemusic;
	private static MediaPlayer tabrakan;
	private static MediaPlayer shout;
	public static boolean isMusicPlayed;

	public static void startMenuMusic(Context context) {
		menumusic = MediaPlayer.create(context, R.raw.musik_menu);
		menumusic.setLooping(true);
		menumusic.start();
		GameSettingHolder.soundenabled = true;
		isMusicPlayed = true;
	}
	
	public static void stopMenuMusic() {
		if(menumusic!=null) {
			menumusic.release();
			GameSettingHolder.soundenabled = false;
			isMusicPlayed = false;
		}
	}

	public static void startGameMusic(Context context) {
		gamemusic = MediaPlayer.create(context, R.raw.musik_game);
		gamemusic.setLooping(true);
		gamemusic.start();
		GameSettingHolder.soundenabled = true;
		isMusicPlayed = true;
	}

	public static void stopGameMusic() {
		if(gamemusic!=null) {
			gamemusic.release();
			GameSettingHolder.soundenabled = false;
			isMusicPlayed = false;
		}
	}
	
	public static void startTabrakan(Context context) {
		//jika sound dienable dan bunyi tabrakan yang lain udah selesai baru bisa distart lagi
		if(tabrakan==null) {//jika null
			tabrakan = MediaPlayer.create(context, R.raw.kelereng_hit);
		}
		if (GameSettingHolder.soundenabled && !tabrakan.isPlaying()) {
			tabrakan = MediaPlayer.create(context, R.raw.kelereng_hit);
			tabrakan.start();
		}
	}
	public static void startShout(Context context, int id) {
		//jika sound dienable dan bunyi tabrakan yang lain udah selesai baru bisa distart lagi
		if(shout==null) {//jika null
			if (id==1){
				shout = MediaPlayer.create(context, R.raw.mangstab);				
			}
			else if (id==2){
				shout = MediaPlayer.create(context, R.raw.pestagundu);				
			}
			else if (id==3){
				shout = MediaPlayer.create(context, R.raw.habisdah);				
			}

		}
		if (GameSettingHolder.soundenabled && !shout.isPlaying()) {
			if (id==1){
				shout = MediaPlayer.create(context, R.raw.mangstab);
				shout.start();			
			}
			else if (id==2){
				shout = MediaPlayer.create(context, R.raw.pestagundu);
				shout.start();				
			}
			else if (id==3){
				shout = MediaPlayer.create(context, R.raw.habisdah);
				shout.start();		
			}
		}
	}		
	
	public static void changeMusicFromGameToMain(Context context) {
		if(GameSettingHolder.soundenabled){
			GameSound.stopGameMusic();
			GameSound.startMenuMusic(context);
		}
	}
	
	public static void changeMusicFromMainToGame(Context context) {
		if(GameSettingHolder.soundenabled){
			GameSound.stopMenuMusic();
			GameSound.startGameMusic(context);
		}
	}
}
